/*
 * Licensed to the Tai Ping Jin Ke
 *
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称：太平金科-基础组件封装层-登录通-缺省实现
 * 版权说明：本软件属太平金融科技服务(上海)有限公司所有，在未获得太平金融科技服务(上海)有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.taipingframework.boot.dlt.webapi.controller;

import com.taiping.dlt.pojo.bo.AuthLoginBO;
import com.taipingframework.boot.cache.constant.DurationConstant;
import com.taipingframework.boot.dlt.webapi.handler.DltHandler;
import com.taiping.dlt.pojo.query.AuthLoginQO;
// import com.taipingframework.boot.dlt.webapi.vo.AuthLoginVO;
import com.taipingframework.boot.web.client.BrowserCookie;
import com.taipingframework.boot.web.client.ReactiveCookieScene;
import com.taipingframework.boot.web.response.result.ApiResult;
import com.taipingframework.boot.web.response.status.ApiStatusEnum;
import com.taipingframework.boot.web.support.ConfigHelper;
import com.taipingframework.utility.exception.ApplicationException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
// import org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@Slf4j
@RestController
@RequestMapping("/api/dlt/security")
@RequiredArgsConstructor
public class SecurityController {

    private final DltHandler dltHandler;

    /**
     * 判断当前会话是否处于登录状态
     */
    @PostMapping("/check")
    public Mono<ApiResult<Boolean>> check() {
        return dltHandler.check()
                .onErrorResume(e -> Mono.error(new ApplicationException(e)));
    }

    /**
     * 登录接口
     */
    @PostMapping("/login")
    public Mono<ApiResult<String>> login(@RequestBody AuthLoginQO loginQO) {
        return dltHandler.login(loginQO)
                .onErrorResume(e -> Mono.error(new ApplicationException(e)));
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public Mono<ApiResult<Boolean>> logout() {
        return dltHandler.logout()
                .onErrorResume(e -> Mono.error(new ApplicationException(e)));
    }

    /**
     * 获取当前登录用户
     */
    @GetMapping("/current")
    public Mono<ApiResult<AuthLoginBO>> current() {
        return dltHandler.current()
                .onErrorResume(e -> Mono.error(new ApplicationException(e)));
    }

    /**
     * 配置信息加密
     */
    @GetMapping("/encrypt")
    public Mono<ApiResult<String>> configEncryptHandler(@RequestParam("value") String value) {
        return ReactiveCookieScene.readWebCookie(value)
                .map(cookie -> {
                    if (StringUtils.isEmpty(cookie.getValue())) {
                        String secretValue = ConfigHelper.encrypt(value);
                        BrowserCookie browserCookie = new BrowserCookie(value, secretValue, DurationConstant.MIN_3.instance());
                        return ReactiveCookieScene.writeWebCookie(browserCookie)
                                .then(ApiResult.<String>create())
                                .map(result -> result.setStatus(ApiStatusEnum.SUCCESS).setResult(secretValue));
                    } else {
                        return ApiResult.<String>create()
                                .map(result -> result.setStatus(ApiStatusEnum.SUCCESS).setResult(cookie.getValue()));
                    }
                })
                .flatMap(apiResultMono -> apiResultMono)
                .onErrorResume(e -> Mono.error(new ApplicationException(e)));
    }

}
